package com.chain.BrotherExchange.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.model.ExtractCoinAddressList;
import com.chain.BrotherExchange.utils.Androids;

import java.util.List;

public class ExtractAddressAdapter extends RecyclerView.Adapter<ExtractAddressAdapter.ViewHolder> {

    private final int mPageType;

    private List<ExtractCoinAddressList.DataBeanX.DataBean> mAddressList;

    public ExtractAddressAdapter(int type) {
        this.mPageType = type;
    }

    @NonNull
    @Override
    public ExtractAddressAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
        return new ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_extract_address, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ExtractAddressAdapter.ViewHolder holder, int position) {
        Activity activity = (Activity) holder.itemView.getContext();
        ExtractCoinAddressList.DataBeanX.DataBean bean = mAddressList.get(position);
        holder.mAddressTextView.setText(bean.getAddress());
        holder.mCoinTypeTextView.setText(bean.getToken_name());
        holder.mRemarkTextView.setText(bean.getMark());

        if (mPageType == C.VALUE_GET_ADDRESS) {
            holder.itemView.setOnClickListener(v -> {
                activity.setResult(Activity.RESULT_OK, new Intent()
                        .putExtra(C.EXTRA_COIN_ADDRESS, bean.getAddress()));
                activity.finish();
            });
        }

        // 删除地址
        holder.mDeleteAddressView.setOnClickListener(v -> {
            int uid = Androids.LocalConfig.getUid();
            String token = Androids.LocalConfig.getToken();
            String id = String.valueOf(bean.getId());
            DataRepository.get().deleteExtractAddress(token, uid, id, result -> {
                if (result.isSucceed()) {
                    mAddressList.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position, getItemCount() - position);
                }
            });
        });
    }

    public ExtractCoinAddressList.DataBeanX.DataBean getItem(int position) {
        return mAddressList.get(position);
    }

    @Override
    public int getItemCount() {
        return mAddressList != null ? mAddressList.size() : 0;
    }

    public void setAddressList(List<ExtractCoinAddressList.DataBeanX.DataBean> result) {
        this.mAddressList = result;
        notifyDataSetChanged();
    }

//    public void setDeleteClickListener(ListView.OnItemClickListener onItemClickListener) {
//        this.mOnItemClickListener = onItemClickListener;
//    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final View mDeleteAddressView;
        private final TextView mRemarkTextView;
        private final TextView mAddressTextView;
        private final TextView mCoinTypeTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mCoinTypeTextView = itemView.findViewById(R.id.coin_type_text);
            mAddressTextView = itemView.findViewById(R.id.address_text_view);
            mRemarkTextView = itemView.findViewById(R.id.remark_text);
            mDeleteAddressView = itemView.findViewById(R.id.delete_address_view);
        }
    }
}
